﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Qiang.Controls
{

    [Serializable]
    public class MapFunction
    {
        
        public MapFunction()
        {
            Cursor = Cursors.Default;
        }

        #region 属性

        [NonSerialized]
        private MapControl _mapControl;

        public MapControl MapControl
        {
            get { return _mapControl; }
            set { _mapControl = value; }
        }



        private bool _isActived = false;

        public bool IsActived
        {
            get { return _isActived; }
            set { _isActived = value; }
        }


        Cursor _cursor;

        /// <summary>
        /// 鼠标指针图像.
        /// </summary>
        public Cursor Cursor
        {
            get { return _cursor; }
            set { _cursor = value; }
        }


        #endregion


        #region 方法

        public virtual void Active()
        {
            this.Deactive();
            this._isActived = true;
        }

        public virtual void Deactive()
        {
            this._isActived = false;
        }

        public void Paint(MapPaintArgs args)
        { 
            //当窗体控件只重绘一部分窗体，而不是整个窗体都重绘时
            //此时参数args的矩形视图区域，也就是窗体的重绘区域已经发生变化了
            //所以再进行坐标转换时就不能用当前绘制区域，而要采用控件本身大小的区域
            //即不能用args的转换坐标方法，而是采用map的转换坐标方法

            this.OnPaint(args);
        }

        public void DoMouseDown(MapMouseArgs e)
        {
            this.OnMouseDown(e);
        }

        public void DoMouseUp(MapMouseArgs e)
        {
            this.OnMouseUp(e);
        }

        public void DoMouseMove(MapMouseArgs e)
        {
            this.OnMouseMove(e);
        }

        public void DoMouseWheel(MapMouseArgs e)
        {
            this.OnMouseWheel(e);
        }


        public void DoMouseClick(MapMouseArgs e)
        {
            this.OnMouseClick(e);
        }


        public void DoKeyDown(KeyEventArgs e)
        {
            this.OnKeyDown(e);
        }

        public void DoKeyUp(KeyEventArgs e)
        {
            this.OnKeyUp(e);
        }


        protected virtual void OnPaint(MapPaintArgs args)
        { 
        }
               

        protected virtual void OnMouseDown(MapMouseArgs e)
        { 
        }

        protected virtual void OnMouseUp(MapMouseArgs e)
        { 
        }


        protected virtual void OnMouseMove(MapMouseArgs e)
        {
            //this.MapControl.Invalidate();
        }

        protected virtual void OnMouseWheel(MapMouseArgs e)
        {
            Rectangle r = e.Control.Map.CurrentView;

            int w = r.Width;
            int h = r.Height;

            if (e.Delta > 0)
            {
                r.Inflate((int)(-r.Width / 4 * e.Control.ScaleFactor), (int)(-r.Height / 4 * e.Control.ScaleFactor));
                // The mouse cursor should anchor the geographic location during zoom.
                r.X += (int)(((e.X / 2) - w / 4) * e.Control.ScaleFactor);
                r.Y += (int)(((e.Y / 2) - h / 4) * e.Control.ScaleFactor);                
                e.Control.Map.ResetExtent(r);
            }
            else  //缩小
            {
                r.Inflate((int)(r.Width / 2 * e.Control.ScaleFactor), (int)(r.Height / 2 * e.Control.ScaleFactor));
                r.X += (int)((w / 2 - e.X) * e.Control.ScaleFactor);
                r.Y += (int)((h / 2 - e.Y) * e.Control.ScaleFactor);
              
                e.Control.Map.ResetExtent(r);
            }

            e.Control.Invalidate();
        }


        protected virtual void OnMouseClick(MapMouseArgs e)
        { 
        }

        protected virtual void OnKeyDown(KeyEventArgs e)
        { 
        }

        protected virtual void OnKeyUp(KeyEventArgs e)
        {
        }

        #endregion 





    }
}
